﻿using System.IO;

using NUnit.Framework;
using Ninject;

using System.Net.DnsToolkit.Bastard.PacketGenerator;
using System.Net.DnsToolkit.Configuration;
using System.Net.DnsToolkit.Packet;
using System.Net.DnsToolkit.Serializer;
using System.Net.DnsToolkit.Util;
using System.Net.DnsToolkit.TestUnit.Helper;

namespace System.Net.DnsToolkit.TestUnit.Bastard.PacketGenerator {
    [TestFixture]
    public class DomainPointerDualLoopTest {

        [Test]
        public void TestGetBytes() {

            var generator = new DomainPointerDualLoop();
            var bs = generator.GetBytes();
            var result = BitStringHelper.ByteArrayToString(bs);

            Assert.AreEqual("000000000002000000000000097175657374696F6E30C01C00010001097175657374696F6E31C00C00010001", result, "result");
            Assert.AreEqual(0xC0, bs[22], "Domain compression pointer");
            Assert.AreEqual(28, bs[23], "Question 1 Offset 0");
            Assert.AreEqual(0xC0, bs[38], "Domain compression pointer");
            Assert.AreEqual(12, bs[39], "Question 0 Offset 0");

        }

        [Test]
        public void TestReadMessage() {

            var generator = new DomainPointerDualLoop();
            var bs = generator.GetBytes();

            var message = DnsReader.Read(new MemoryStream(bs));
            // DarkDns toolkit is immune to DomainPointerDualLoop attack
            Assert.AreEqual(2, message.Questions.Count, "count");
            Assert.AreEqual("question0.question1.question0.", message.Questions[0].QName.Name, "question0");
            Assert.AreEqual("question1.question0.question1.", message.Questions[1].QName.Name, "question1");

        }

    }
}
